import Vue from 'vue'
import Router from 'vue-router'
import Login from "../view/Login"
import Home from "../view/Home";
import Welcome from "../view/Welcome";
import Users from "../view/user/Users";
import Rights from "../view/power/Rights";
import Roles from "../view/power/Roles";
import Cate from "../view/goods/Cate";
import Params from "../view/goods/Params";
import List from "../view/goods/List";
import Add from "../view/goods/Add";
import Edit from "../view/goods/Edit";
import Order from "../view/order/Order";
import Report from "../view/report/Report";

//处理防止停留当前页面点击提示错误信息处理办法
const originalPush = Router.prototype.push;
Router.prototype.push = function(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router);

 const router = new Router({
  routes:[
    {
      path:'/login',
      component:Login
    },
    {
      path:'/home',
      component:Home,
      redirect:'/welcome', //重定向路径
      //子路由路径
      children:[
        {path:'/welcome',component:Welcome},
        {path:'/users',component:Users},
        {path: '/rights',component: Rights},
        {path: '/roles',component: Roles},
        {path: '/categories',component: Cate},
        {path: '/params',component: Params},
        {path: '/goods',component: List},
        {path: '/goods/add',component: Add},
        {path: '/goods/edit',component: Edit},
        {path: '/orders',component: Order},
        {path: '/reports',component: Report}
        ]
    }
  ]
});

 //挂载路由导航守卫
router.beforeEach((to,from,next) => {
  //to 访问的路径
  //from 代表从哪个路径跳转而来
  //next 是一个函数，表示方行
      // next() 方行 next("/login") 强制跳转页面
  if (to.path === '/login') return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem("token");
  if (!tokenStr) return next("/login");
  next();
})

 export default router;
